﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Web;
using MySql.Data.MySqlClient;

namespace app.scrum
{
    public class DatabaseManager
    {        
        public static readonly string ODBC = "ODBC";
        public static readonly string MySql = "MySql";
        public static string connType = MySql;

        void oleTest()
        {
            /*OleDbConnection con = null;
            try
            {
                string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                               @"DataSource=D:\training\project\web\app\scrum\db\scrum.mdb";
                con = new OleDbConnection(conString);
                con.Open();
                string strSql = "select * from product";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(strSql, con);
                DataSet dSet = new DataSet();
                dAdapter.Fill(dSet, "PersonTable");
                this.Response.Write(dSet.ToString());
            }
            catch (Exception ex)
            {
                this.Response.Write("Error in connection : " + ex.Message);
                if (con != null)
                {
                    con.Close();
                }
            }*/
        }

        public static DbConnection getDbConnection()
        {
            if (String.Equals(connType, ODBC))
            {
                return getOdbcConnection();
            }
            else if (String.Equals(connType, MySql))
            {
                return getMySqlConnection();
            }
            return null;
        }

        public static OdbcConnection getOdbcConnection()
        {
            OdbcConnection con = new OdbcConnection("DSN=scrum");
            con.Open();
            return con;
        }

        public static MySqlConnection getMySqlConnection()
        {
            string conStr = "server=localhost;user id=root;password=root;database=scrum;pooling=false;charset=utf8";
            MySqlConnection con = new MySqlConnection(conStr);
            con.Open();
            return con;
        }

        public static DbDataAdapter createDbDataAdapter(DbCommand cmd)
        {
            if (String.Equals(connType, ODBC))
            {
                return new OdbcDataAdapter((OdbcCommand)cmd);
            }
            else if (String.Equals(connType, MySql))
            {
                return new MySqlDataAdapter((MySqlCommand)cmd);
            }
            return null;
        }

        public static void closeResource(DbConnection con, DbCommand cmd)
        {
            closeResource(con, cmd, (DbDataReader)null);
        }

        public static void closeResource(DbConnection con, DbCommand cmd, DbDataReader reader)
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (con != null)
            {
                con.Close();
            }
        }

        public static void closeResource(DbConnection con, DbCommand cmd, DbDataAdapter adapter)
        {
            if (adapter != null)
            {
                adapter.Dispose();
            }
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (con != null)
            {
                con.Close();
            }
        }
    }
}